package async;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * @author fei.fang
 * @date 2021/8/30
 **/
public class PipedStreamDemo {

    public static void main(String[] args) throws IOException {
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader();
        PipedInputStream inputStream = new PipedInputStream();
        PipedOutputStream outputStream = new PipedOutputStream();

        writer.connect(reader);
        Thread printThread = new Thread(new PrintDemo(reader));
        printThread.start();

        int receive;

        try {
            while ((receive = System.in.read()) != -1) {
                writer.write(receive);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }
    }

    public static class PrintDemo implements Runnable {

        private PipedReader in;

        public PrintDemo(PipedReader in) {
            this.in = in;
        }

        @Override
        public void run() {
            int receive;

            try {
                while ((receive = in.read()) != -1) {
                    System.out.println((char) receive);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
